/**
 * Definition for singly-linked list.
 * class ListNode {
 *     val: number
 *     next: ListNode | null
 *     constructor(val?: number, next?: ListNode | null) {
 *         this.val = (val===undefined ? 0 : val)
 *         this.next = (next===undefined ? null : next)
 *     }
 * }
 */
class ListNode {
    val: number
    next: ListNode | null
    constructor(val?: number, next?: ListNode | null) {
        this.val = (val === undefined ? 0 : val)
        this.next = (next === undefined ? null : next)
    }
}

function mergeTwoLists(list1: ListNode | null, list2: ListNode | null): ListNode | null {
    let l3: any = new ListNode() ;
    if(list1?.next === null && list2?.next === null ){
        return l3;
    }
    
    let arr: any = [];
    
    while (list1) {
        arr.push(list1.val);
        list1 = list1.next;
    }
    while (list2) {
        arr.push(list2.val);
        list2 = list2.next;
    }
    l3.next = arr[0]||null ;
    
    let l4 = l3;
    let node: ListNode;
    arr = arr.sort();
    for (let i = 1; i < arr.length; i++) {
        node = new ListNode( arr[i] );
        l3.next = node;
        l3 = l3.next;
    }   
    l3.next = null;
    return l4;
};

// let l1: ListNode = { val: 1, next: { val: 2, next: { val: 4, next: null } } }
// let l2: ListNode = { val: 1, next: { val: 3, next: { val: 4, next: null } } }
let l1:ListNode = {val: 0, next:null};
let l2:ListNode = {val: 0, next:null};
let l3: any = mergeTwoLists(l1, l2);
while(l3){
    console.log("l3.val:",l3.val);
    l3 = l3.next;
}
console.log(l3);